<template>

	<view class="main">


		<u-navbar :bgColor="bgColor" :fixed="false" title="患者详细信息" placeholder="true" @rightClick="rightClick" :autoBack="true"></u-navbar>

		<view class="info">



			<!-- 表单部分 -->
			<u--form labelWidth="100" :model="formData" :rules="rules" ref="uForm">

				<u-form-item label="账号" prop="account" borderBottom>
					<u--input placeholder="请输入" border="none" v-model="formData.account" type="number"></u--input>
				</u-form-item>

				<u-form-item label="姓名" prop="name" borderBottom>
					<u--input placeholder="请输入姓名" v-model="formData.name" border="none"></u--input>
				</u-form-item>

				<u-form-item label="昵称" prop="nickname" borderBottom>
					<u--input placeholder="请输入" border="none" v-model="formData.nickname" type="number"></u--input>
				</u-form-item>


				<u-form-item label="电话" prop="phone" borderBottom>
					<u--input placeholder="请输入" v-model="formData.phone" border="none"></u--input>
				</u-form-item>


				<u-form-item label="性别" prop="gender" borderBottom>

					<u-radio-group v-model="formData.gender">
						<u-radio name=1 value=1>男</u-radio>
						<view style="margin-left: 50rpx;">
							<u-radio name=2 value=2>女</u-radio>
						</view>

					</u-radio-group>
				</u-form-item>

				<u-form-item label="年龄" prop="age" borderBottom>
					<u--input placeholder="请输入" border="none" v-model="formData.age"></u--input>
				</u-form-item>


				<u-form-item label="血型" prop="blood_type" borderBottom>
					<u--input placeholder="请输入" border="none" v-model="formData.blood_type" type="number"></u--input>
				</u-form-item>


				<u-form-item label="备注" prop="remark" borderBottom>
					<u--input placeholder="请输入" border="none" v-model="formData.remark" type="number"></u--input>
				</u-form-item>

				<u-form-item label="头像" prop="avatar" borderBottom>
					<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple :maxCount="1"></u-upload>
				</u-form-item>
			</u--form>
			<u-button @click="setUserInfoFuc" :custom-style="{ backgroundColor: '#27CCBB', marginTop:'100rpx',color: '#fff', borderRadius: '40rpx', height:'80rpx'}" text="保 存"></u-button>


		</view>
	</view>

</template>

<script>
	import api_patient from '@/api/patient.js';

	export default {
		components: {

		},
		async onLoad(option) {
			this.user = option.id;
			console.log('useruseruseruser', this.user);
		},

		async onShow() {
			this.access_token = uni.getStorageSync('access_token');
			if (!this.access_token) {
				uni.navigateTo({
					url: '/pages/auth/login'
				});
			} else {
				this.getUserInfoFuc(); //获取患者信息
			}
		},




		data() {
			return {
				fileList1: [],
				user: null,
				access_token: null,
				valueTime: Number(new Date()),
				showTime: false,

				rules: {
					name: {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},



					gender: {
						type: 'string',
						required: true,
						message: '性别不能为空',
						trigger: ['blur', 'change']
					},


				},
				bgColor: 'rgba(0, 31, 63, 0)',
				formData: {

				},
			};
		},
		methods: {


			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)

					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: 'http://192.168.88.237:8788/api/upload/file', // 仅为示例，非真实的接口地址
						filePath: url,
						name: 'file',
						formData: {
							type: 'image'
						},
						success: (res) => {
							setTimeout(() => {
								const parsedData = JSON.parse(res.data);
								resolve(parsedData.data.url)
							}, 1000)
						}
					});
				})
			},



			//获取患者信息
			async getUserInfoFuc() {
				var per = {
					user: this.user
				}
				const res = await api_patient.getUserInfo(per);
				if (res.code == 1) {
					this.formData = res.data.info
					this.fileList1 = [
						{ url: res.data.info.avatar }
					]
					this.$set(this.formData, 'gender', String(this.formData.gender))
				} else {
					this.$util.msg(res.message);
					return;
				}
			},




			//设置患者信息
			async setUserInfoFuc() {
				var per = {
					user: this.user,
					name: this.formData.name,
					gender: this.formData.gender,
				}
				if (this.formData.age) {
					per.age = this.formData.age
				}
				if (this.formData.blood_type) {
					per.blood_type = this.formData.blood_type
				}
				if (this.formData.remark) {
					per.remark = this.formData.remark
				}
				if (this.fileList1.length > 0) {
					per.avatar = this.fileList1[0].url
				}
				const res = await api_patient.setUserInfo(per);
				if (res.code == 1) {
					uni.$u.toast('成功')
					this.getUserInfoFuc();
				} else {
					this.$util.msg(res.message);
					return;
				}
			},

		}
	};
</script>

<style scoped lang="scss">
	.info {
		width: 100%;
		min-height: 1000rpx;
		background: #FFFFFF;
		top: 540rpx;
		padding: 50rpx 32rpx 32rpx 32rpx;
	}

	::v-deep .u-form-item {
		line-height: 50px;
	}

	.avatar-upload {
		display: flex;
		align-items: center;
		justify-content: center;
		gap: 20rpx;
		flex-direction: column;
	}

	.upload-placeholder {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.avatar-preview {
		width: 120rpx;
		height: 120rpx;
		border-radius: 50%;
		object-fit: cover;
		border: 1px solid #e5e5e5;
	}
</style>